package com.ethan.cj.ssm.controller;


import com.ethan.cj.ssm.entity.Dept;
import com.ethan.cj.ssm.service.DeptService;
import com.ethan.cj.ssm.util.DateUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/dept")
public class DeptController {
    @Autowired
    private DeptService deptService;
    @RequestMapping("/select")
    public void selectall(HttpServletRequest request, HttpServletResponse response){
        response.setCharacterEncoding("UTF-8");

        int currpage;//定义当前页
        String currPage=request.getParameter("page");//从页面获得当前页
        if(null==currPage||"".equals(currPage))
        {
            currpage=1; //第一次进入时，默认当前页为1，否则为空无法操作
        }
        else{
            currpage=Integer.parseInt(currPage);

        }
        //从页面获得每页展示多少条数
        int pagesize=Integer.parseInt(request.getParameter("limit"));
        //从页面得到部门名称
        String dname=null;
        if(request.getParameter("dname")!=null&&!"".equals(request.getParameter("dname")))
        {
            dname=request.getParameter("dname");
        }
        int did=0;
        if(request.getParameter("did")!=null&&!"".equals(request.getParameter("did")))
        {
            did=Integer.parseInt(request.getParameter("did"));
        }

        //查询得到所有部门，都放到集合中
        List<Dept> list=deptService.selectall(currpage,pagesize,did,dname);

        //获得部门总条数
        int totalCount=deptService.countDept(did,dname);

        //把得到的数据都转换为json格式
        Map<Object, Object> json= new HashMap<>();
        json.put("code",0);
        json.put("msg","");
        json.put("count",totalCount);

        json.put("data",list);
        PrintWriter out=null;
        try {
            out=response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //json传到页面
        out.print(json);
    }
    @RequestMapping(value="/insert")
    @ResponseBody
    public Map<Object, Object> insertNotice(Dept dept, HttpServletRequest request)
    {

        int bo= deptService.insertDept(dept);
        //用json传条提示判断的信息
        Map<Object, Object> json = new HashMap<>();
        json.put("message",bo);
        return json;
    }

    /**
     * 转到增加公告的页面
     * @return
     */
    @RequestMapping("/add")
    public String addDept()
    {
        return "adddept";
    }

    /**
     *
     *通过id 查询一条公告，返回到修改的界面，作为回显
     * @param did
     * @param model
     * @return
     */
    @RequestMapping("/selectDeptByid")
    public String selectNoticeByid(int did, Model model)
    {
        Dept dept=deptService.selectDeptByid(did);
        model.addAttribute(dept);
        return "updatedept";
    }

    /**
     *
     * 修改一条公告，返回提醒判断值
     * @param dept
     * @param request
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public Map<Object, Object> updateDept(Dept dept, HttpServletRequest request)
    {

        int bo=deptService.updateDept(dept);
        Map<Object, Object> json=new HashMap<>();
        json.put("message",bo);
        return json;
    }

    /**
     * 删除一条公告，并返回提示信息
     * @param did
     * @param model
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Map<Object, Object> deleteNotice(int did, Model model)
    {
        int bo=deptService.deleteDept(did);
        Map<Object, Object> json=new HashMap<>();
        json.put("message",bo);
        return json;
    }


    /**
     *
     * 批量删除几条公告信息，并返回提示判断信息
     * @param s
     * @param model
     * @return
     */
    @RequestMapping("/deletevolume")
    @ResponseBody
    public Map<Object, Object> deletevolumeNotice(String s, Model model)
    {
        int bo=0;
        String a[]=s.split(",");
        for(int i=0;i<a.length;i++)
        {
            bo=deptService.deleteDept(Integer.parseInt(a[i]));
        }
        Map<Object, Object> json=new HashMap<>();
        json.put("message",bo);
        return json;

    }
}
